<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>用户维护</title>

<link rel="stylesheet" type="text/css" href="../../css/cm-default.css" />
<link rel="stylesheet" type="text/css" href="../../css/main.css" />
<link rel="stylesheet" type="text/css" href="../../lib/ligerUI/skins/Aqua/css/ligerui-all.css" />
<link rel="stylesheet" type="text/css" href="../../lib/ligerUI/skins/ligerui-icons.css" />
<link rel="stylesheet" type="text/css" href="../../lib/bootstrap/css/bootstrap.css"/>
<script type="text/javascript" src="../../lib/jquery/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="../../lib/ligerUI/js/ligerui.all.js" ></script>
<script type="text/javascript" src="../../js/common.js" ></script>
<script type="text/javascript" src="../../lib/autoInput/AutoInput.js" ></script>
<script type="text/javascript" src="../../js/pub.js"></script>
<script type="text/javascript" src="../../lib/bootstrap/js/bootstrap.js" ></script>
<script type="text/javascript" src="../../lib/md5.js"></script>
<script type="text/javascript">
var defGrid;
var userid;
$(function(){
    defGrid = $("#query_list").ligerGrid({
        columns:[
            {display:'操作',isSort:false,width:60,render:function(rowdata,rowindex,value) {
                var h = "";
                if (!rowdata._editing) {
                    h += "<a href='javascript:void(0);' class='opt-link' onclick='beginEdit(" + rowindex + ")'>修改</a> ";
                    h += "<a href='javascript:void(0);' class='opt-link' onclick='deleteRow(" + rowindex + ")'>删除</a> ";
                } else {
                    h += "<a href='javascript:void(0);' class='opt-link' onclick='endEdit(" + rowindex + ")'>提交</a> ";
                    h += "<a href='javascript:void(0);' class='opt-link' onclick='cancelEdit(" + rowindex + ")'>取消</a> ";
                }
                return h;
            },frozen:true},
            {display:'账号',name:'account',width:80,minWidth:60,align:'left',editor:{type:'text'}},
            {display:'用户名',name:'name',width:100,minWidth:60,align:'left',editor:{type:'text'}},
            {display:'所属机构',name:'orgid',width:80,minWidth:60,data:get_org_list,render:switcher_org,editor:{type:'select',data:get_org_list,textField:'name',valueField:'value',selectBoxHeight:50}},
            {display:'状态',name:'status',width:70,data:status,render:switcher,editor:{type:'select',data:getStatus,textField:'name',valueField:'value',selectBoxHeight:50}},

            {display:'密码修改',isSort:false,width:100,render:function(rowdata,rowindex,value){
                return "<a href='javascript:void(0);' class='opt-link' onclick='open_Modfiy_PW(" + rowindex + ")'>密码修改</a>";
            }},
            {display:'角色设置',isSort:false,width:100,render:function(rowdata,rowindex,value){
                return "<a href='javascript:void(0);' class='opt-link' onclick='open_Set_role(" + rowindex + ")'>角色设置</a>";
            }},
            {display:'说明',name:'description',width:200,align:'left',editor:{type:'text'}}
        ],
        pageSize:10,
        usePager: true,
        sortName:'id',
        width:'100%',
        height:'100%',
        dataAction:'server',
        checkbox:false,
        rownumbers:false,
        fixedCellHeight:false,
        colDraggable:true,
        enabledEdit:true,
        clickToEdit:false,
        onSelectRow:function(rowdata, rowindex){$("#txtrowindex").val(rowindex);},
        sqlid:"rbac.select.dict.def.queryUser",
        url: '/comm/q',
        contentType : "application/json; charset=utf-8",
        param_status:'rbac.status',
        param_org:'org_list'
    });
    $("#pageloading").hide();
    $("#org_comboboxS").ligerComboBox({
        autocomplete: true,
        valueField:'value',
        textField:'name',
        data:get_org_list,
        onSelected:function(v,t) {
            $("#orgidS").val(v);
        }
    });
});
function beginEdit(rowid) {
    defGrid.beginEdit(rowid);
}
function cancelEdit(rowid) {
    defGrid.cancelEdit(rowid);
}
function endEdit(rowid) {
    defGrid.endEdit(rowid);
    var row = defGrid.getRow(rowid);
    toModify(JSON.stringify(row));
}
function deleteRow(rowid) {
    $.ligerDialog.confirm('您确定要删除该项数据吗？', function(r) {
        if (!r)
            return;
        var row = defGrid.getRow(rowid);
        if (row['__status'] !== 'add')
            toDel(JSON.stringify(row));
        defGrid.deleteRow(rowid);
    })
}
function switcher(item) {
    return paramRender(item, defGrid.options.param_status, item.status);
}
var getStatus = new function () {
    return getData('rbac.status');
}
function switcher_org(item) {
    return paramRender(item, defGrid.options.param_org, item.orgid);
}
var get_org_list=new function () {
    return getData('org_list');
}

//用户的角色维护弹出窗
function open_Set_role(rowid) {
    userid = defGrid.getRow(rowid)['id'];;
//    var userName
//    userName = defGrid.getRow(rowid)['name'];
//    parent.f_addTab('user_role'+userName+'','角色维护('+userName+')','./html/sysmgt/user_role.html?userid='+userid+'');
    $.ligerDialog.open ({ height: 300,width:450, title:'角色资源设置',url: '/html/sysmgt/user_role.html?userid='+userid+'', isResize:true  });

}
function toQuery(item, page, pageSize) {
    var param = new Object();
    param.account=$("#AccountCode").val();
    param.name = $("#userName").val();
    param.status = $("#dictStatus").val();
    // l-list 线性结构，默认；t-tree 树型结构
    param.rt='l';
    //重置页码
    defGrid.set({
        page : 1,
        newPage : 1,
        postData : param
    });
    // grid 重新加载数据
    defGrid.loadData();
}
//新增用户弹出窗口
var addDialog;
function openUserDef() {
    toReset('#new_user_def input,select,textarea');
    addDialog = $.ligerDialog.open({
        target:$("#new_user_def"),
        title:"新增用户",
        width:450,
        height:420
    });
}
//密码修改窗口
function open_Modfiy_PW(rowid) {
    userid = defGrid.getRow(rowid)['id'];;
    if (userid) {
        toReset('#up_dict_pw input,select,textarea');
        addDialog = $.ligerDialog.open({
            target:$("#up_dict_pw"),
            title:"密码修改",
            width:350,
            height:250
        });
    }
}
//修改密码时，两次密码一致性校验。不一致时，弹出窗口
function toupdatepw()
{
    var pwd1=$("#password1").val();
    var pwd2=$("#password2").val();
    if(pwd1!=pwd2)
    {
        $.ligerDialog.success('两次密码不一致请重新输入！');
        return;
    }
    var parampw = new Object();
    parampw.id=userid;
    parampw.pwd =hex_md5(pwd1).toUpperCase() ;

    var param = JSON.stringify(reqJson('rbac.update.dict.def.updateuserpd',JSON.stringify(parampw)));

    ajaxSubmit("/comm/u", param, function(data) {
        if (data) {
            if (parseInt(data['status']) > 0) {
                $.ligerDialog.success('保存成功');
                addDialog.hide();
            } else {
                $.ligerDialog.error('保存失败');
            }
        }
    });}
function validate() {
    var valid = true;

    $('*[id$="S"],*[id$="S_txt"]').each(function(index, element) {

        if (!$(this).rules()) return;
        if (!$(element).valid()) {
            valid = false;
            return;
        }
    });
    if (!valid) return false;

    return true
}
// 新增保存处理
function toAdd() {
    if (!validate())
    {
        return;
    }
    var param = new Object();
    param.code=$("#accountS").val();
    param.account=$("#accountS").val();
    param.name = $("#newNameS").val();
    param.password = hex_md5($("#passwordS").val()).toUpperCase();
    param.orgid=$("#orgidS").val();
    param.status = $("#newStatus").val();
    param.desc=$("#newDesc").val();
    var data = JSON.stringify(reqJson('rbac.add.dict.def.addUser',JSON.stringify(param)));
    ajaxSubmit("/comm/a", data, function(data) {
        if (data) {
            if (parseInt(data['status']) > 0) {
                $.ligerDialog.success('保存成功', '操作完成', function() {
                    addDialog.hide();
                    toQuery();
                });
            } else if (parseInt(data['status']) == -1) {
                $.ligerDialog.error('用户[' + $("#newName").val() + ']已存在<br>不能重复添加','错误');
            } else {
                $.ligerDialog.error('保存失败');
            }
        }
    });
}

function toModify(row) {

    var param = JSON.stringify(reqJson('rbac.update.dict.def.updateuser',row));
    ajaxSubmit("/comm/u", param, function(data) {
        if (data) {
            if (parseInt(data['status']) > 0) {
                $.ligerDialog.success('保存成功');
            } else {
                $.ligerDialog.error('保存失败');
            }
        }
    });
}
//删除时，判断此用户下是否已设置角色信息，删除时提示，将删除用户的角色设置信息
//确认删除时，将用户下的角色设置信息一并删除。
function toDel(row) {
    var param = JSON.stringify(reqJson('rbac.delete.dict.def.deleteuser',row));
    ajaxSubmit("/comm/d", param, function(data) {
        if (data) {
            if (parseInt(data['status']) > 0) {
                $.ligerDialog.success('删除成功');
            } else {
                $.ligerDialog.error('删除失败');
            }
        }
    });
}
</script>
<style type="text/css">
    label{margin: 5px 20px 5px 15px;}
    .fields{height:65px;padding-top:5px;background-color:#F0EEED}
    .dict-info{width: 260px;padding: 10px;}
    input.diff {margin:3px 0;height:1px;line-height:25px;}
    .lineD {width:99%;float:left;position:relative;overflow:hidden;margin:10px 0 2px 2px}
    .labelD {width:30%;position:relative;float:left;padding-top:3px;}
    .dplD {width:69%;position:relative;float:left;}
    .title{font-size:14px;font-weight:bold;background-position:left;padding-left:18px;}
</style>
</head>
<body style="padding:0;">
<div id="pageloading"></div>
<div id="layout_main">
    <div position="center" id="a_center" title="用户列表">
        <div id="query_cond" class="fields">
        	<div style="padding-left:15px;padding-top:5px;">
 			<button type="button" onclick="openUserDef();" class="btn btn-primary btn-xs ">
   				&nbsp;<span class="glyphicon glyphicon-plus">新增</span>
    		</button>
   			</div>
            <label>用户账号</label><input type="text" class="s-text txt-def" id="AccountCode" />
            <label>用户名称</label><input type="text" class="s-text txt-def" id="userName" />
            <label>状态</label><select id="dictStatus" param="rbac.status" first=""></select>
            <button type="button" onclick="toQuery();" style="margin-left:25px;" class="form_Button btn btn-primary btn-xs">查询</button>
            <button type="button" onclick="toReset('#query_cond input,select,textarea');" class="form_Button btn btn-primary btn-xs">清空</button>
        </div>
        <div id="query_list"></div>
    </div>
</div>
<div id="new_user_def" style="display:none;overflow:hidden;">
    <form name="basic-form" method="post" id="basic_form" class="liger-form" data-validate="{}">
        <div class="lineD">
            <div class="labelD"><label>账号</label></div>
            <div class="dplD"><input type="text" ltype="text" class="required s-text txt-def" id="accountS" /></div>
        </div>
        <div class="lineD">
            <div class="labelD"><label>用户名</label></div>
            <div class="dplD"><input type="text" ltype="text" class="required s-text txt-def" id="newNameS" /></div>
        </div>
        <div class="lineD">
            <div class="labelD"><label>密码</label></div>
            <div class="dplD"><input type="password" ltype="text" class="required s-text txt-def" id="passwordS" /></div>
        </div>

        <div class="lineD">
            <div class="labelD"><label>机构</label></div>
            <div class="dplD">
                <input type="text" id="org_comboboxS" ltype="combobox" class="required diff" /></div>
            <input type="hidden" id="orgidS" />
        </div>


        <div class="lineD">
            <div class="labelD"><label>状态</label></div>
            <div class="dplD"><select class="txt-def" id="newStatus" param="rbac.status" default="1" style="margin-right: 30px;"></select></div>
        </div>
        <div class="lineD">
            <div class="labelD"><label>说明</label></div>
            <div class="dplD"><textarea cols="24" rows="5" id="newDesc"></textarea></div>
        </div>
        <div class="lineD" style="margin-top:15px;padding-left:30%;">
            <button type="button" onclick="toAdd();" class="form_Button btn btn-primary btn-xs">保存</button>
    		<button type="button" onclick="toReset('#up_dict_pw input,select,textarea')" class="form_Button btn btn-primary btn-xs">清空</button>
        </div>
    </form>
</div>

<div id="up_dict_pw" style="display:none;overflow:hidden;">

    <div class="lineD">
        <div class="labelD"><label>新密码</label></div>
        <div class="dplD"><input type="password" class="s-text txt-def" id="password1" /></div>
    </div>
    <div class="lineD">
        <div class="labelD"><label>确认密码</label></div>
        <div class="dplD"><input type="password" class="s-text txt-def" id="password2" /></div>
    </div>
    <div class="lineD" style="margin-top:15px;padding-left:30%;">
        <button type="button" onclick="toupdatepw();" class="form_Button btn btn-primary btn-xs">保存</button>
    	<button type="button" onclick="toReset('#up_dict_pw input,select,textarea')" class="form_Button btn btn-primary btn-xs">清空</button>
    </div>
</div>
</body>
</html>